﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace GzipManagerPro.IO.Helpers
{
    [System.Diagnostics.DebuggerStepThrough]
    internal static class Serializer
    {
        public static byte[] SerializeToBinary(this object instance)
        {
            
            MemoryStream stream = new MemoryStream();
            BinaryFormatter formatter = new BinaryFormatter();

            formatter.Serialize(stream, instance);

            return stream.ToArray();
        }

        public static T FromBinary<T>(this byte[] buffer)
        {
            MemoryStream memoryStream = new MemoryStream(buffer);
            BinaryFormatter formatter = new BinaryFormatter();
            T result = (T)formatter.Deserialize(memoryStream);
            memoryStream.Close();

            return result;
        }
    }
}
